package com.tian.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tian.common.CommonResult;
import com.tian.dto.BaseReqPageDto;
import com.tian.dto.BaseRespPageDto;
import com.tian.dto.ChargeUserLoginResDto;
import com.tian.dto.UserPointChangeRespDto;
import com.tian.entity.ChargeUserPoint;
import com.tian.entity.PointsChangeRecord;
import com.tian.mapper.ChargeUserPointMapper;
import com.tian.mapper.PointsChangeRecordMapper;
import com.tian.service.UserPointService;
import com.tian.util.DateUtils;
import com.tian.util.UserCacheUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * {@code @description:} 用户积分服务实现类
 *
 * @author tianwc 公众号：Java后端技术全栈
 * 在线刷题 1200+java面试题和1000+篇技术文章：<a href="https://woaijava.cc/">博客地址</a>
 * {@code @date:} 2024-09-20 10:06
 * {@code @version:} 1.0
 */
@Service
public class UserPointServiceImpl implements UserPointService {
    @Resource
    private ChargeUserPointMapper chargeUserPointMapper;
    @Resource
    private PointsChangeRecordMapper pointsChangeRecordMapper;
    @Override
    public CommonResult<Integer> getUserPoint() {
        ChargeUserLoginResDto user = UserCacheUtil.getUser();
        Long id = user.getId();
        ChargeUserPoint chargeUserPoint = chargeUserPointMapper.selectByUserId(id);
        if (chargeUserPoint != null) {
            return CommonResult.success(chargeUserPoint.getPoint());
        }
        return CommonResult.success(0);
    }

    @Override
    public CommonResult<BaseRespPageDto<List<UserPointChangeRespDto>>> changeList(BaseReqPageDto baseReqPageDto) {
        ChargeUserLoginResDto user = UserCacheUtil.getUser();
        Long userId = user.getId();
        List<Long> userIdList = new ArrayList<>();
        userIdList.add(userId);
        Page<PointsChangeRecord> page = PageHelper.startPage(baseReqPageDto.getPageNum(), baseReqPageDto.getPageSize());
        List<PointsChangeRecord> pointsChangeRecordList = pointsChangeRecordMapper.selectByUserIdList(userIdList);
        List<UserPointChangeRespDto> userPointChangeRespDtoList = new ArrayList<>();
        for (PointsChangeRecord pointsChangeRecord : pointsChangeRecordList) {
            UserPointChangeRespDto userPointChangeRespDto = new UserPointChangeRespDto();
            userPointChangeRespDto.setChangePoint(pointsChangeRecord.getType() == 0 ?
                    "+" + pointsChangeRecord.getPoint() : "-" + pointsChangeRecord.getPoint());
            userPointChangeRespDto.setCreateTime(DateUtils.format(pointsChangeRecord.getCreateTime(), DateUtils.FORMAT_SECOND));
            userPointChangeRespDto.setRemark(pointsChangeRecord.getRemark());
            userPointChangeRespDtoList.add(userPointChangeRespDto);
        }
        BaseRespPageDto<List<UserPointChangeRespDto>> baseRespPageDto = new BaseRespPageDto<>();
        baseRespPageDto.setTotalCount(page.getTotal());
        baseRespPageDto.setPageSize(baseReqPageDto.getPageSize());
        baseRespPageDto.setPageNum(baseReqPageDto.getPageNum());
        baseRespPageDto.setTotalPage(page.getPages());
        baseRespPageDto.setData(userPointChangeRespDtoList);
        return CommonResult.success(baseRespPageDto);
    }

}
